package at.ac.univie.zsu.aguataplan.domain;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Version;
import javax.validation.constraints.Size;

/**
 * @author gerry
 * 
 */
@Entity
public class EventAttribute implements Comparable<EventAttribute> {

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	@Column
	private Long id;

	@Version
	@Column
	private Integer version;

	@Size(min = 1, max = 30)
	@Column(nullable = false, unique = true)
	private String name;

	@Column
	@Size(min = 0, max = 1)
	private String shortcut;

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	private Set<Event> refEvent = new HashSet<Event>();

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	private Set<EventAttributeValue> refEventAttributeValue = new HashSet<EventAttributeValue>();

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public EventAttribute() {

	}

	// ----------------------------------------------------
	// COMPARABLE
	// ----------------------------------------------------

	@Override
	public int compareTo(EventAttribute o) {
		if (this.name.compareToIgnoreCase(o.name) < 0) {
			return -1;
		} else if (this.name.compareToIgnoreCase(o.name) > 0) {
			return 1;
		}
		return 0;
	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	public boolean addEvent(Event event) {
		return this.refEvent.add(event);
	}

	public boolean deleteEvent(Event event) {
		Iterator<Event> it = refEvent.iterator();

		long i;
		i = event.getId();
		long j = -1;

		while (it.hasNext()) {
			Event e = it.next();
			j = e.getId();
			if (i == j) {
				it.remove();
				return true;
			}
		}

		return false;
	}

	public boolean addEventAttributeValueString(EventAttributeValueString eventAttributeValueString) {
		return this.refEventAttributeValue.add(eventAttributeValueString);
	}

	public boolean deleteEventAttributeValueString(EventAttributeValueString eventAttributeValueString) {
		Iterator<EventAttributeValue> it = refEventAttributeValue.iterator();
		long i;
		i = eventAttributeValueString.getId();
		long j = -1;

		while (it.hasNext()) {
			EventAttributeValue e = it.next();
			j = e.getId();
			if (i == j) {
				it.remove();
				return true;
			}
		}
		return false;
	}

	public void cleanEvent() {
		this.refEvent = new HashSet<Event>();
	}

	public String toString() {
		return this.name;
	}

	public String toStringRelation() {
		StringBuffer sb = new StringBuffer();
		for (EventAttributeValue ea : refEventAttributeValue) {
			sb.append(name + " -> " + ea.toString());
			sb.append("\n");
		}

		return sb.toString();
	}

	public String toStringExtended() {
		StringBuilder sb = new StringBuilder();
		sb.append("Id: ").append(getId()).append(", ");
		sb.append("Name: ").append(getName()).append(", ");
		// sb.append("RefEventAttributeValue: ")
		// .append(getRefEventAttributeValue() == null ? "null" :
		// getRefEventAttributeValue().size()).append(", ");
		// sb.append("RefEvent: ").append(getRefEvent() == null ? "null" :
		// getRefEvent().size()).append(", ");
		sb.append("Shortcut: ").append(getShortcut()).append(", ");
		sb.append("Version: ").append(getVersion());
		return sb.toString();
	}

	public String toStringExport() {
		return this.name;
	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getShortcut() {
		return shortcut;
	}

	public void setShortcut(String shortcut) {
		this.shortcut = shortcut;
	}

	public Set<Event> getRefEvent() {
		return refEvent;
	}

	public void setRefEvent(Set<Event> refEvent) {
		this.refEvent = refEvent;
	}

	// public Set<EventAttributeValueString> getRefEventAttributeValueString() {
	// return refEventAttributeValueString;
	// }
	//
	// public void
	// setRefEventAttributeValueString(Set<EventAttributeValueString>
	// refEventAttributeValueString) {
	// this.refEventAttributeValueString = refEventAttributeValueString;
	// }

	public Set<EventAttributeValue> getRefEventAttributeValue() {
		return refEventAttributeValue;
	}

	public void setRefEventAttributeValue(Set<EventAttributeValue> refEventAttributeValue) {
		this.refEventAttributeValue = refEventAttributeValue;
	}

}
